package main.flex.helper
{
	import flash.events.Event;
	import flash.utils.getQualifiedClassName;
	
	import main.flex.control.MainController;
	import main.flex.control.MediaController;
	import main.flex.model.MediaModel;
	import main.flex.value.MediaListTO;
	import main.flex.view.MediaView;
	
	import mx.controls.Alert;
	
	import org.servebox.foundry.model.ModelLocator;
	import org.servebox.foundry.observation.IObservable;
	import org.servebox.foundry.observation.Notification;
	

	public class MediaViewHelper extends MainViewHelper
	{
		[Bindable]
		private var espaceId		:		int;
		private var listMedia		:		MediaListTO;
		
		public function MediaViewHelper()
		{
			super();
		}	
		
		[Bindable("userLogged")]
		public function getEspaceId():int
		{
			return espaceId;
		}
		
		override public function registerToModels() : void
		{
			super.registerToModels();
			ModelLocator.getInstance().getModel( MediaController.MEDIA_MODEL_ID ).registerObserver( this );
		}
		
		[Bindable("userLogged")]
		public function getMediaList( userId : int ):void
		{
			getMediaController().getMedia ( userId );
		}
		
		override public function initDatas():void
		{
			getMediaController().getMedia( espaceId );
		}
		
		override public function userLoggedNotification_Fired( o : IObservable, n :Notification ) : void
		{
			if(user.user[0].loggedIn == "true")
			{
				espaceId = getMainModel().user.user[0].espaceId;
				initDatas();
			}
		}
		
		[Bindable("mediaListChanged")]
		public function get mediaList() : MediaListTO
		{
			getMediaView().coverflow.mediaData = getMediaModel().mediaList.mediaList;
			return getMediaModel().mediaList;
		}
		
		public function mediaListChangedNotification_Fired( o : IObservable, n :Notification ) : void
		{
			dispatchEvent( new Event("mediaListChanged") );
		}
		
		protected function getMediaController ( ) : MediaController
		{
			return MainController.getInstance().getSubController( getQualifiedClassName( MediaController ) ) as MediaController;
		}
		
		/**
		* return a reference to MediaModel
		*/
		
		protected function getMediaModel() : MediaModel
		{
			return MediaModel ( ModelLocator.getInstance().getModel( MediaController.MEDIA_MODEL_ID ));
		}
		
		private function getMediaView( ) : MediaView
		{
			return MediaView( getView() );
		}
		
		
	}
}
